//
//  GWPublishViewController.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/6.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWPublishViewController.h"
#import "GWVerticalButton.h"
#import <POP.h>
#import "GWPublishWordController.h"
#import "GWMyNavigationController.h"
@interface GWPublishViewController ()

@end
static CGFloat AnimitionDelay =0.05;
@implementation GWPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.userInteractionEnabled =NO;
    
    NSArray *images =@[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
    NSArray *titles =@[@"发视频",@"发图片",@"发段子",@"发声音",@"审帖",@"离线下载"];
    NSInteger maxClumns =3;
    CGFloat btnW =72;
    CGFloat btnH =btnW +30;
    CGFloat btnStarY =(GWScreenH -btnH *2) *0.5;
    CGFloat btnStarX =25;
    CGFloat btnMargin =(GWScreenW -btnStarX*2 -maxClumns*btnW)/(maxClumns-1);
    for(int i=0;i<images.count;i++)
    {
        GWVerticalButton *btn =[[GWVerticalButton alloc]init];
        [self.view addSubview:btn];
        
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font =[UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(btnOnTouch:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag =i;
        NSInteger clu = i%maxClumns;
        NSInteger row =i/maxClumns;
        CGFloat btnX =btnStarX +(btnMargin+btnW)*clu;
        CGFloat btnEndY = btnStarY +btnH*row;
        CGFloat btnStarY = btnEndY -GWScreenH;
        btn.frame =CGRectMake(btnX, btnStarY, btnW, btnH);
        
        POPSpringAnimation *anmi =[POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anmi.toValue =[NSValue valueWithCGRect:CGRectMake(btnX, btnEndY, btnW, btnH)];
        anmi.springBounciness =10;
        anmi.springSpeed =10;
        anmi.beginTime =CACurrentMediaTime() +AnimitionDelay*i;
        [btn pop_addAnimation:anmi forKey:nil];
       
    }
    
    UIImageView *slognView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
    slognView.centerX =GWScreenW*0.5;
    slognView.y =GWScreenH*0.2 -GWScreenH;
    [self.view addSubview:slognView];
    
    POPSpringAnimation *anmi =[POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anmi.toValue =[NSValue valueWithCGPoint:CGPointMake(GWScreenW*0.5, GWScreenH*0.2)];
    anmi.springBounciness =10;
    anmi.springSpeed =10;
    anmi.beginTime =CACurrentMediaTime()+images.count*AnimitionDelay;
    [slognView pop_addAnimation:anmi forKey:nil];
    [anmi setCompletionBlock:^(POPAnimation *anmi, BOOL completion) {
        self.view.userInteractionEnabled =YES;
    }];
}
-(void)btnOnTouch:(UIButton *)btn
{
    [self cancelWithCompletionBlock:^{
        
        if (btn.tag==2) {
            GWMyNavigationController *nav =[[GWMyNavigationController alloc]initWithRootViewController:[[GWPublishWordController alloc] init]];
            UIViewController *vc =[UIApplication sharedApplication].keyWindow.rootViewController;
            [vc presentViewController:nav animated:YES completion:nil];
        }
        NSLog(@"打开了%@",btn.titleLabel.text);
    }];
}
- (IBAction)cancel {
    
    [self cancelWithCompletionBlock:nil];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self cancelWithCompletionBlock:nil];
}

-(void)cancelWithCompletionBlock:(void(^)())completion
{
  
    self.view.userInteractionEnabled =NO;
    
    for (int i=2; i<(int)self.view.subviews.count; i++) {
        UIView *view =self.view.subviews[i];
        
        POPBasicAnimation *anmi =[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        anmi.toValue =[NSValue valueWithCGPoint:CGPointMake(view.centerX, view.centerY+GWScreenH)];
        anmi.beginTime =CACurrentMediaTime()+ (i-2)*AnimitionDelay;
        [view pop_addAnimation:anmi forKey:nil];
        
        if (i==self.view.subviews.count-1) {
            [anmi setCompletionBlock:^(POPAnimation *anmia, BOOL comple) {
               
                [self dismissViewControllerAnimated:NO completion:nil];
                !completion? :completion();
            }];
        }
    }
}
@end
